package com.smartao.base.department;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smartao.base.employee.EmployeeDao;
import com.smartao.base.position.PositionDao;
import com.smartao.core.dao.BaseDao;
import com.smartao.core.service.BaseEntityService;

@Service
public class DepartmentServiceImpl extends BaseEntityService<Department, Long> implements DepartmentService {

	@Autowired
	private DepartmentDao departmentDao;
	
	@Autowired
	private PositionDao positionDao;
	
	@Autowired
	private EmployeeDao employeeDao;

	@Override
	protected BaseDao<Department, Long> getDao() {
		return departmentDao;
	}
	
	@Override
	public void save(Department department){
		// 更新
		if(department.getId()!=null){
			departmentDao.save(department);
		}
		// 新增
		else{
			Department parent = department.getParent();
			if (parent == null) {
				department.setLeftKey(1);
				department.setRightKey(2);
				departmentDao.save(department);
			} else {
				Integer pRight = parent.getRightKey();
				Long dimensionId = parent.getDimension().getId();
				departmentDao.batchUpdateLeftKey(pRight, dimensionId);
				departmentDao.batchUpdateRightKey(pRight, dimensionId);
				
				department.setLeftKey(pRight);
				department.setRightKey(pRight + 1);
				departmentDao.save(department);
			}
		}
		
	}
	
	@Override
	public List<Department> getAllChild(Department department) {
		return departmentDao.getAllChild(department);
	}

	@Override
	public List<Department> getAllChild(Long departmentId) {
		Department department = findOne(departmentId);
		return departmentDao.getAllChild(department);
	}

	@Override
	public List<Department> getDirectChild(Department parent) {
		return departmentDao.findByParent(parent.getId());
	}

	@Override
	public List<Department> getDirectChild(Long departmentId) {
		return departmentDao.findByParent(departmentId);
	}

	@Override
	public List<Department> getPath(Department department) {
		return departmentDao.getPath(department);
	}

	@Override
	public List<Department> getPath(Long departmentId) {
		Department child = findOne(departmentId);
		return getPath(child);
	}
	
	@Override
	public String getPathString(Department department) {
		List<Department> path = departmentDao.getPath(department);
		StringBuffer sb = new StringBuffer();
		for(Department dept : path){
			sb.append(dept.getName()).append("/");
		}
		sb.append(department.getName());
		return sb.toString();
	}

	@Override
	public String getPathString(Long departmentId) {
		return getPathString(findOne(departmentId));
	}
	
	@Override
	public Department findRoot(Long dimensionId) {
		return departmentDao.findRoot(dimensionId);
	}

	@Override
	public Department findRoot() {
		return departmentDao.findRoot();
	}
	
	@Override
	public Long getDierctChildCount(Long id) {
		return departmentDao.getDierctChildCount(id);
	}

	@Override
	public Long getDierctChildCount(Department department) {
		return departmentDao.getDierctChildCount(department.getId());
	}
	
}